// JavaScript Document
var FormChecker = 
{
    check : function () 
    {
        var checked = true;
        // CHECK PERSONAL INFO
        if (document.getElementById("first_name").value.trim() == "") 
        {
            FormChecker.reportError(document.getElementById("first_name"), "Required field");
            checked = false;
        }
        else {
            FormChecker.removeError(document.getElementById("first_name"));
        }
        if (document.getElementById("last_name").value.trim() == "") 
        {
            FormChecker.reportError(document.getElementById("last_name"), "Required field");
            checked = false;
        }
        else {
            FormChecker.removeError(document.getElementById("last_name"));
        }
        if (document.getElementById("email").value.trim() != "") 
        {
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            if (reg.test(document.getElementById("email").value.trim()) == true) {
                FormChecker.removeError(document.getElementById("email"));
            }
            else {
                FormChecker.reportError(document.getElementById("email"), "Not valid email format");
			}
        }
        else 
        {
            FormChecker.reportError(document.getElementById("email"), "Required field");
            checked = false;
        }
        // CHECK ACCOUNT INFO
        if (document.getElementById("username").value.trim() != "") 
        {
            if (document.getElementById("username").value.trim().length >= 6) {
                FormChecker.removeError(document.getElementById("username"));
            }
            else 
            {
                FormChecker.reportError(document.getElementById("username"), "6 chars at least");
                checked = false;
            }
        }
        else 
        {
            FormChecker.reportError(document.getElementById("username"), "Required field");
            checked = false;
        }
        if (document.getElementById("password").value.trim() != "") 
        {
            if (document.getElementById("password").value.trim().length >= 6) {
                FormChecker.removeError(document.getElementById("password"));
            }
            else 
            {
                FormChecker.reportError(document.getElementById("password"), "6 chars at least");
                checked = false;
            }
        }
        else 
        {
            FormChecker.reportError(document.getElementById("password"), "Required field");
            checked = false;
        }
        if (document.getElementById("confirm_password").value != document.getElementById("password").value) 
        {
            FormChecker.reportError(document.getElementById("confirm_password"), "Check for typo");
            checked = false;
        }
        return checked;
    },
    reportError : function (origin, errorString) 
    {
        if (document.getElementById(origin.id + "_err")) 
        {
            document.getElementById(origin.id + "_err").parentNode.removeChild(document.getElementById(origin.id + "_err"));
        }
        var errorContainer = document.createElement("span");
        errorContainer.id = origin.id + "_err";
        errorContainer.className = "error_box";
        errorContainer.appendChild(document.createTextNode(errorString));
        origin.parentNode.appendChild(errorContainer);
    },
    removeError : function (origin) 
    {
        if (document.getElementById(origin.id + "_err")) 
        {
            document.getElementById(origin.id + "_err").parentNode.removeChild(document.getElementById(origin.id + "_err"));
        }
    }
};

var JSAdapter = {
	
	adapt : function() 
	{
		var js = document.createElement("input");
		js.type = "hidden";
		js.name = "js";
		js.value = "1";
		document.getElementById("signup_form").appendChild(js);
	}

}
